

package com.zb.myspringbootquick.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class BasicController {

	// http://127.0.0.1:8080/hello?name=lisi
	@RequestMapping("/hello")
	@ResponseBody
	public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
		return "Hello " + name;
	}

	// http://127.0.0.1:8080/user
	@RequestMapping("/user")
	@ResponseBody
	public User user() {
		User user = new User();
		user.setName("thereon");
		user.setAge(666);
		return user;
	}

	// http://127.0.0.1:8080/save_user?name=newName&age=11
	@RequestMapping("/save_user")
	@ResponseBody
	public String saveUser(User u) {
		return "user will save: name=" + u.getName() + ", age=" + u.getAge();
	}

	// http://127.0.0.1:8080/html
	@RequestMapping("/html")
	public String html() {
		return "index.html";
	}

	@ModelAttribute
	public void parseUser(@RequestParam(name = "name", defaultValue = "unknown user") String name
			, @RequestParam(name = "age", defaultValue = "12") Integer age, User user) {
		user.setName("zhangsan");
		user.setAge(18);
	}
}
